package com.gogant.spider.http;

import com.gogant.spider.core.URLPicker;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML.Attribute;
import javax.swing.text.html.HTML.Tag;
import javax.swing.text.html.HTMLEditorKit.ParserCallback;

/**
 * Http URL提取器：从满足条件URL的页面中提取符合指定要求的URL
 */
public class HttpURLPicker implements URLPicker<HttpResponse> {

    @Override
    public List<URL> pick(HttpResponse resp) {
        if (!resp.isOK() || resp.getContentType() == null || !resp.getContentType().startsWith("text/html")) {
            return null;
        }
        final List<URL> urlList = new ArrayList<URL>();
        final URL base = resp.getURL();
        HtmlHelper.parseHtml(resp, new ParserCallback() {
            @Override
            public void handleStartTag(Tag t, MutableAttributeSet a, int pos) {
                handleTag(t, a, pos);
            }

            @Override
            public void handleSimpleTag(Tag t, MutableAttributeSet a, int pos) {
                handleTag(t, a, pos);
            }

            private void handleTag(Tag t, MutableAttributeSet a, int pos) {
                String href = null;
                if (t == Tag.A || t == Tag.LINK) {
                    href = (String) a.getAttribute(Attribute.HREF);
                } else if (t == Tag.IMG || t == Tag.SCRIPT) {
                    href = (String) a.getAttribute(Attribute.SRC);
                } else {
                    return;
                }
                if (href == null || href.trim().length() == 0) {
                    return;
                }
                try {
                    URL url = new URL(base, href);
                    urlList.add(url);
                } catch (Throwable e) {
                }
            }
        });
        return urlList;
    }
}
